package org.dtrd.modules.evaluation.controller;


import cn.hutool.core.collection.CollectionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.entity.bean.MmpiDetail;
import org.dtrd.modules.evaluation.entity.bean.MmpiListInfo;
import org.dtrd.modules.evaluation.entity.bean.MmpiListRequest;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdMmpiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 * 明苏尼达多相个性评估 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-15
 */
@Api(tags = "量表评估")
@RestController
public class MmpiController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdMmpiService mmpiService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation(value = "查询最近一次的明苏尼达多相个性评估详情", response = MmpiDetail.class)
    @GetMapping("/105/48/1054801")
    public Result<?> getMmpi(@RequestParam Integer patientId){
        if (isIntParamInvalid(patientId)) {
            return error(STATE_PARAMS_MISSING);
        }
        MmpiDetail info = mmpiService.getLatestMmpi(patientId);
        String logContent = String.format("查询最近一次的明苏尼达多相个性评估详情，查询patientId：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(info);
    }

    @ApiOperation(value = "查询明苏尼达多相个性评估列表", response = MmpiListInfo.class)
    @PostMapping("/105/48/1054803")
    @AutoLog(value="查询明苏尼达多相个性评估列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getMmpiList(@RequestBody MmpiListRequest request){
        Integer patientId = request.getPatientId();
        if (isIntParamInvalid(patientId)) {
            return error(STATE_PARAMS_MISSING);
        }
        List<MmpiListInfo> list = mmpiService.getMmpiList(request);
        String logContent = String.format("查询明苏尼达多相个性评估列表, 查询条件: %s", request);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(list);
    }

    @ApiOperation("新增明苏尼达多相个性评估")
    @PostMapping(value = {"/105/48/1054802", "/100/17/1001714"})
    public Result<?> saveOrUpdateMmpi(@RequestBody MmpiDetail dto){
        if (CollectionUtil.isEmpty(dto.getInput())) {
            return error(STATE_PARAMS_MISSING);
        }
        MmpiDetail info = mmpiService.saveOrUpdateMmpi(dto);
        String logContent = String.format("新增明苏尼达多相个性评估, 新增用户id: %d", dto.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_ADD);
        return Result.OK(info);
    }

    @ApiOperation("删除明苏尼达多相个性评估")
    @DeleteMapping("/105/48/1054804")
    @AutoLog(value="删除明苏尼达多相个性评估", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeMmpi(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = mmpiService.removeMmpi(id);
        String logContent = String.format("删除明苏尼达多相个性评估, 删除记录id: %d", id);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
